Retry

class Retry(val retryConfiguration: RetryConfiguration, val retryWhen: (Throwable) -> Boolean = ::retryIfServerFault)

Instantiate a "retrier", than can be used to wrap any call to GoodReads. GoodReads tends to return 5XX errors codes sporadically, so this is useful when automating tasks.

Samples

import ch.derlin.grmetafetcher.GoodReadsLookup
import ch.derlin.grmetafetcher.GoodReadsMetadata
import ch.derlin.grmetafetcher.GoodReadsPaginatedSearchResults
import ch.derlin.grmetafetcher.Retry
import ch.derlin.grmetafetcher.RetryConfiguration
fun main() { 
   //sampleStart 
   // A Retry must be instantiated with a configuration.
// It also supports a custom lambda to tell when to retry. By default, it retries on any 5XX server error.
var retrier: Retry
// Two configurations exist: EXPONENTIAL, and SIMPLE
retrier = Retry(RetryConfiguration.EXPONENTIAL)
// Each default configuration can be customized, e.g.
retrier = Retry(RetryConfiguration.EXPONENTIAL.copy(maxRetries = 10))
// Or a completely custom configuration can be provided:
retrier = Retry(RetryConfiguration(maxRetries = 3, interval = 200, multiplier = 1f))

// Once we have a Retry instance, we can use .run, like this:
val result = retrier.run { GoodReadsMetadata.lookup(title = "Project Hail Mary") }
println(result.toCompilableString()) 
   //sampleEnd
}

Constructors

Link copied to clipboard
fun Retry(retryConfiguration: RetryConfiguration, retryWhen: (Throwable) -> Boolean = ::retryIfServerFault)

Types

Link copied to clipboard
object Companion

Functions

Link copied to clipboard
fun <T> run(body: () -> T): T

Run some code, and retry if a "retriable" exception is raised (see Retry.retryWhen).

Properties

Link copied to clipboard
val retryConfiguration: RetryConfiguration

Configure the max retries, intervals, etc.

Link copied to clipboard
val retryWhen: (Throwable) -> Boolean

Lambda called to determine if the exception should be retried. Retry on error code 500 from GoodReads by default (see Retry.Companion.retryIfServerFault.

Sources

Link copied to clipboard